﻿using System.Linq;
using MaxStoreErp.Core.Commands;
using MaxStoreErp.Core.Extensions;
using MaxStoreErp.Core.Interfaces;
using MaxStoreShop.Core.EditorBase;
using MaxStoreShop.Core.Interception;
using MaxStoreShop.Domain.Shop;
using MaxStoreShop.Logic.ServicesContracts.Directories;
using MaxStoreShop.Logic.ServicesContracts.Shop;
using Microsoft.Practices.Unity;

namespace MaxStoreShop.ShopModule.DebtorRules.DebtorRuleEditor
{
    internal class DebtorRuleEditorPresenter :
        EditorPresenterBase<DebtorRuleEditorView, DebtorRuleEditorViewModel, DebtorRule>
    {
        [Dependency]
        public ICharacterService CharacterService { get; set; }

        [Dependency]
        public IDebtorRuleService DebtorRuleService { get; set; }

        protected override void OnSave()
        {
            base.OnSave();
            DebtorRuleService.SaveRule(ViewModel.SelectedItem);
            Container.Resolve<IDocumentManager>().Back();
        }

        protected override void OnViewModelSet()
        {
            base.OnViewModelSet();
            ViewModel.AddCharacterCommand = new RelayCommand(AddNewCharacter);
        }

        void AddNewCharacter()
        {
            Container.Resolve<ICharacterEditor>().CreateNewCharacter();
        }

        protected override void OnDataLoading()
        {
            base.OnDataLoading();
            ViewModel.Characters = CharacterService.GetCharacters().ToObservableCollection();
            if (ViewModel.SelectedItem.Character != null)
                ViewModel.SelectedItem.Character =
                    ViewModel.Characters.First(c => c.Id == ViewModel.SelectedItem.Character.Id);
        }
    }
}
